{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import scipy as sp\n",
    "import scipy.linalg\n",
    "import sympy as sy\n",
    "sy.init_printing() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.set_printoptions(precision=3)\n",
    "np.set_printoptions(suppress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def round_expr(expr, num_digits):\n",
    "    return expr.xreplace({n : round(n, num_digits) for n in expr.atoms(sy.Number)})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font face=\"gotham\" color=\"purple\"> Computation of Determinants</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For $2\\times 2$ matrix $A$, the algorithm of determinant is\n",
    "$$\n",
    "  A=\\left[\\begin{matrix} a & b\\cr c & d \\end{matrix}\\right] \\qquad\\text{is equivalent to}\\qquad \\text{det}  A=ad-bc\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we experiment with SymPy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "a, b, c, d, e, f, g, h, i = sy.symbols('a, b, c, d, e, f, g, h, i', real = True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With defined symbols, the algorithms of $2\\times 2$ and $3\\times 3$ determinants are"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEU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nBR9qd2gIvrT4CNiJ3sESVcKHRtVFNJmY77QoEGhhV+KtLtHELAYz5fpt0Fs8kZxcX1EZMshPJZv2Db1f1b9aPaH9P8KU2RNAv8CTljGddiPqU4AAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle a d - b c$"
      ],
      "text/plain": [
       "a⋅d - b⋅c"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = sy.Matrix([[a, b], [c, d]])\n",
    "A.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAUIAAAAXCAYAAAB+mUZsAAAIcklEQVR4nO2ceWwVVRTGf8giChhxQQGRiAJSRGpRQSRRUDG4BMXEgGsxJkYRFyKLGwEUUGJMMRqXRAOoCcbU4IJoAy4lkqioBQUXXIqyqSAYQBQQ/OO7N286nXnv3en0zbOdL2nu68ydO/eb79wz5565My2mTZtGihQpUjRnHJLAOScAB4ExCZzbFUcATwC1wF7U78lkOFyTWM+CcRvq100xtHWiaes13/ak9IvKLUzDpoxc2hWb3SaOVgmc80xTfpbAuV3xMnAZ8DbwErAfeAO43+z/PKF+haHMlHFcW9uWn2NS+kXlFqZhU0aYdmHbmz2ScIRTgBnAugTO7YJT0QB6F7jUt68M2A18V+hO5cAA4G9gTQxthTmepPSLwi2bhk0ZYdoVq90mjiSmxj8D36AQvZgxzJSVvu3tgN5ADXCgkB3KgUOBvsBqFPU0FANM6Y8eoupXbo45P0JfonIL07CxUE50jnEiSLtitduiQJAjvAZNJ74DdgLbgU+BsVnauQpYAmxFeZh1wH1AS1+9ochQ5jSo19GQL6+rUB+fMv8/Z/4/iCKMUnTdPgdK0HRrC7ALWAEMbEQO2dAPaI2igFI0+LehCKAKOC3gmFbAncjB7AHWA5OAFih62Aj86qmflH6u3HJpCNAWRbdrUaS5nozN7kTXJAmcD7wCbAD+ATajiPYKX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wlfgpGiaSCSxmmOMEWKFM0eqSNMkSJFs8d/WAEORjMf5KkAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle a e i - a f h - b d i + b f g + c d h - c e g$"
      ],
      "text/plain": [
       "a⋅e⋅i - a⋅f⋅h - b⋅d⋅i + b⋅f⋅g + c⋅d⋅h - c⋅e⋅g"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = sy.Matrix([[a, b, c], [d, e, f], [g, h, i]])\n",
    "B.det()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> Cofactor Expansion</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " The $(i,j)$-**cofactor** of $  A$ is denoted as $C_{ij}$ given by\n",
    " $$C_{i j}=(-1)^{i+j} \\operatorname{det} A_{i j} = (-1)^{i+j} M_{ij}$$\n",
    " where $M_{ij}$ is the **minor determinant** which excludes $i$th row and $j$th column."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider a matrix $  A$\n",
    "$$A=\\left[\\begin{array}{rrr}\n",
    "1 & 5 & 0 \\\\\n",
    "2 & 4 & -1 \\\\\n",
    "0 & -2 & 0\n",
    "\\end{array}\\right]$$\n",
    "Any determinants can be expanded long its arbitrary row or column, we expand the determinant via the first row\n",
    "$$\\begin{aligned}\n",
    "\\operatorname{det} A &=1 \\cdot \\operatorname{det}\\left[\\begin{array}{rr}\n",
    "4 & -1 \\\\\n",
    "-2 & 0\n",
    "\\end{array}\\right]-5 \\cdot \\operatorname{det}\\left[\\begin{array}{cc}\n",
    "2 & -1 \\\\\n",
    "0 & 0\n",
    "\\end{array}\\right]+0 \\cdot \\operatorname{det}\\left[\\begin{array}{rr}\n",
    "2 & 4 \\\\\n",
    "0 & -2\n",
    "\\end{array}\\right] \\\\\n",
    "&=1(0-2)-5(0-0)+0(-4-0)=-2\n",
    "\\end{aligned}$$\n",
    "\n",
    "The scalars, $1$, $-5$ and $0$, in front of each minor determinant are the elements of first row of $  A$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In general, the expansions across $i$th row or $j$th column are\n",
    "$$\n",
    "\\operatorname{det} A=a_{i 1} C_{i 1}+a_{i 2} C_{i 2}+\\cdots+a_{i n} C_{i n}\n",
    "$$\n",
    "$$\n",
    "\\operatorname{det} A=a_{1 j} C_{1 j}+a_{2 j} C_{2 j}+\\cdots+a_{n j} C_{n j}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <font face=\"gotham\" color=\"purple\"> A SymPy Example of Determinant Expansion</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider the matrix below and perform a cofactor expansion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}49 & 0 & 61\\\\73 & 22 & 96\\\\2 & 0 & 32\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡49  0   61⎤\n",
       "⎢          ⎥\n",
       "⎢73  22  96⎥\n",
       "⎢          ⎥\n",
       "⎣2   0   32⎦"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = sy.Matrix([[49, 0, 61], [73, 22, 96], [2, 0, 32]]);A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Cofactor expansion with the column which has two zero(s) involves the least computation burden:\n",
    "\n",
    "$$\\operatorname{det} A = a_{12}(-1)^{1+2}C_{12}+a_{22}(-1)^{2+2}C_{22}+a_{32}(-1)^{3+2}C_{32}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use SymPy function for calculationg minors: ```sy.matrices.matrices.MatrixDeterminant.minor(A, i, 1)```. Also we define a function for cofactor expansion:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cof_exp(matrix, c): # c is the col that we want to expand\n",
    "    detA = 0\n",
    "    for i in range(matrix.shape[1]): # matrix.shape[1] is the total number of columns\n",
    "        detA += matrix[i, c]*(-1)**(i+c)*sy.matrices.matrices.MatrixDeterminant.minor(matrix, i, c)\n",
    "    return detA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEAAAAASCAYAAADrL9giAAADDUlEQVR4nO3XW4hVZRQH8N+YEGRWYKbUgzQmGEVWRiiGF4rI0i5TQi9ZkBoUiOagJoqDIGhBFxWMEJHUlzKKIjE1pQyhoBKJihwjbzTmjfJSaGoP6zu5Z5997Bz3vE1/OKx91v72919rnb3+6ztNbW1tujN65L4vxqfYhz9xFN9iPvoUPP8ElmIb/sB5rKmD9yFsxP7E8zPexfAa6xvh6YNJeB/taf/f8QWelcu5Z+7h6fgGm/AbemEY2jAlXe/LrJ+LITiRkhlcI6gsFmMmjuADHMZNeASPY2JBco3wTMBy/Iqt2It+aMEKjE1rzlNdgKvwV8GmCzEHL+H5jH96CqgdoxLhxdAfrTiI20SRKxiDLVigugCN8PyEh/ExzmX8c/CVKHIL3qO6BYqSh3eSHZTzb8UuqZp1YEDi/FLn5Ct7HUffguca4dmCj3ROHjrwZroeXXHmC1AL45PdWef6WtiF07gb1+bujURvbC7JcTGcSfbviiPfAhW04kpcjbtwj0h+UckAjmIWXsX3QgOOYKB4bTfhuZIctdBT6AtsyDqL0CqEo4INeAaHuiCQ1/ELVmJyxt+OVapbo6uwCLdiPT6pOGu1QH80JduCZjEO7+yCQGZinUh2oJg0Q8UoXIuXu4Ajj6mYgR/xVPbGf2nAQTFP7xfz9e2SgYwWY/BDvCiSPiVG72M4kAJtLsmTxQt4Q7TcGNGG/6JeEdyTNrhFtXg1gnHJFo2xU2JM9cAdJTiymIZl+E4k35FfUG8B4Ppkz5YI6PJki0Zd1n+6BEcFs/AadojkC7UlW4DBoueL1izEddiOYyWC2pbsFNyQuzcWI8RZZHsJDpgnRO9r3CtOm4XIToEH8Ao+x24xnvqJk1ezeH0m555/NH24ULzhQuAk4tbM+nVizt+HH4S+dOBm0R5NmJ24L5XnaXGaPCsKPjWftJhCq+hcgM14S/wKQ3ANToqj5WoskRMQ3J4Is2h2QcT26FyAc3hQCNOTQviuSPuuTxwbCwJuhOfGZC8TGlCEz6QCNP3/d7ibo9sX4B/f7r0lksihxAAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle 31812$"
      ],
      "text/plain": [
       "31812"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cof_exp(A,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It's easy to verify the expansion algorithm Sympy's determinant evaluation function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEAAAAASCAYAAADrL9giAAADDUlEQVR4nO3XW4hVZRQH8N+YEGRWYKbUgzQmGEVWRiiGF4rI0i5TQi9ZkBoUiOagJoqDIGhBFxWMEJHUlzKKIjE1pQyhoBKJihwjbzTmjfJSaGoP6zu5Z5997Bz3vE1/OKx91v72919rnb3+6ztNbW1tujN65L4vxqfYhz9xFN9iPvoUPP8ElmIb/sB5rKmD9yFsxP7E8zPexfAa6xvh6YNJeB/taf/f8QWelcu5Z+7h6fgGm/AbemEY2jAlXe/LrJ+LITiRkhlcI6gsFmMmjuADHMZNeASPY2JBco3wTMBy/Iqt2It+aMEKjE1rzlNdgKvwV8GmCzEHL+H5jH96CqgdoxLhxdAfrTiI20SRKxiDLVigugCN8PyEh/ExzmX8c/CVKHIL3qO6BYqSh3eSHZTzb8UuqZp1YEDi/FLn5Ct7HUffguca4dmCj3ROHjrwZroeXXHmC1AL45PdWef6WtiF07gb1+bujURvbC7JcTGcSfbviiPfAhW04kpcjbtwj0h+UckAjmIWXsX3QgOOYKB4bTfhuZIctdBT6AtsyDqL0CqEo4INeAaHuiCQ1/ELVmJyxt+OVapbo6uwCLdiPT6pOGu1QH80JduCZjEO7+yCQGZinUh2oJg0Q8UoXIuXu4Ajj6mYgR/xVPbGf2nAQTFP7xfz9e2SgYwWY/BDvCiSPiVG72M4kAJtLsmTxQt4Q7TcGNGG/6JeEdyTNrhFtXg1gnHJFo2xU2JM9cAdJTiymIZl+E4k35FfUG8B4Ppkz5YI6PJki0Zd1n+6BEcFs/AadojkC7UlW4DBoueL1izEddiOYyWC2pbsFNyQuzcWI8RZZHsJDpgnRO9r3CtOm4XIToEH8Ao+x24xnvqJk1ezeH0m555/NH24ULzhQuAk4tbM+nVizt+HH4S+dOBm0R5NmJ24L5XnaXGaPCsKPjWftJhCq+hcgM14S/wKQ3ANToqj5WoskRMQ3J4Is2h2QcT26FyAc3hQCNOTQviuSPuuTxwbCwJuhOfGZC8TGlCEz6QCNP3/d7ibo9sX4B/f7r0lksihxAAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle 31812$"
      ],
      "text/plain": [
       "31812"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.det()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Actually you can experiment with any random matrices with multiple zeros, the function below has the parameter ```percent=70``` which means $70\\%$ of element are non-zero."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & 0 & 0 & 16 & 14 & 48 & 0\\\\0 & 45 & 17 & 26 & 15 & 50 & 14\\\\0 & 31 & 31 & 0 & 0 & 33 & 43\\\\0 & 0 & 39 & 48 & 0 & 28 & 12\\\\42 & 41 & 25 & 36 & 50 & 25 & 19\\\\36 & 42 & 17 & 36 & 0 & 13 & 20\\\\0 & 11 & 0 & 16 & 27 & 36 & 19\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡0   0   0   16  14  48  0 ⎤\n",
       "⎢                          ⎥\n",
       "⎢0   45  17  26  15  50  14⎥\n",
       "⎢                          ⎥\n",
       "⎢0   31  31  0   0   33  43⎥\n",
       "⎢                          ⎥\n",
       "⎢0   0   39  48  0   28  12⎥\n",
       "⎢                          ⎥\n",
       "⎢42  41  25  36  50  25  19⎥\n",
       "⎢                          ⎥\n",
       "⎢36  42  17  36  0   13  20⎥\n",
       "⎢                          ⎥\n",
       "⎣0   11  0   16  27  36  19⎦"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = sy.randMatrix(r = 7, min=10, max=50, percent=70);B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calculate determinants with our user-defined function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKsAAAASCAYAAAAkLS6kAAAGK0lEQVR4nO3aa6wdVRUH8N+FYq1FRQVsYpCXKEiVgqFSkFJtiak02FZriLGAD9CE2CASIFjIRT+IhvJQEqViaAMkFOojCBcVoaEQURKxGkCheLlKUYo8iq0t5XHLh7Wnd87cmXvPzDkFmtx/Mtlz9l5775n/rL32Wmufnt7eXmMYw86AXbo83mfxQ9yN/2Ebrqs5xsLUbxu+UtJ+aq696nolJ/+uNM4v8Ci24Hncgy8bnYNj8TP8B1tT+Vt8qkS2B1/CH7ARm/FnLMKuFePX5WxA9Xs/2aU5mvbJUIezE1LbOvFt+nETphUFx7U5ebtYjMOwKU1+cM3++wiCNmH3Cpk1uKii7Vh8Arfl6hbgR4KwVfgX3o35uBqzk8y2kvEW4zt4GrekMfbE4ZiBvoL8crHYnsIK/B+zcAWm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ZeIOGn801OVsTirLUmKbRVA2T7iI/by+AdZWoWBlOEI85D2ChCoX4c3Cgg2OMFaGc4WfugbHa40+i1gtFO8gvElErXlMTuXAKHNmWJiedbnhKa9uIdtey7IRrwXqcjY+lXspR1a/fZzXM8DaIo5By66bk8zy9HtFxRgLRMDSZ+TA6gKhqH8SFnUkRZXaV+DtIgDM43h8UpwiFbMIbysZ68g09yaR5O4Eh2oN3jLsiyvTfadxRFPU5ezuVJ6O9xTkZ+MYseP+PqvstmWdmy6G8o7TDJ3aPK36ZKoJssBq6QgypwgleUUQtKhEZsDwk6Wz8FF8S/iB9wmlmJfGOs3wLe92sQgfEEntQ0VwtVVEvWVWb672OVsgIuZVIlW1UQQmJwjL3YdLOpyjkz51OFspTrpm4W/ivxtPipz7HBGonWfI1++6sk4RypHHAYac5H/qnrIeIgKX0QKrLO+5q2q/9y7DlfUpQfxiQfZRQjluxXeV50pX4iSR850gAtCrhWUdqJh7ivY5W4UPCBdpmvBPNwh36dp0lZ041Zmjkz51OBsUC/kMwdk8vEUEvH34gfiDy3b0jP1FcAw7C94IhwJjGENbGFPWMew0eBV4wuqmKd96ygAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle -147269151168$"
      ],
      "text/plain": [
       "-147269151168"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cof_exp(B,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then verify the result of using determinant method  ```.det()```. We can see indeed cofactor expansion works!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKsAAAASCAYAAAAkLS6kAAAGK0lEQVR4nO3aa6wdVRUH8N+FYq1FRQVsYpCXKEiVgqFSkFJtiak02FZriLGAD9CE2CASIFjIRT+IhvJQEqViaAMkFOojCBcVoaEQURKxGkCheLlKUYo8iq0t5XHLh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lOVBYiqXiY6wRKZu1Sa7Trfoi4YevF/7hA0JBLhVK0tvh+E3RhLNzhNuwTPA2ER8RbtP1+H5+gqKy/gMP17j+3cHLZdv/I4YCkCb4oPCX1hmeSirDGcI3fkhEnc+WyDyXyn78pdC2xZAlnpqrHxTb19kieFsocq7r8DGxxTE8mOgWsmBx+g4afzTU5WyGCMhuFrtRv1h89wvlfkIYlO0uVNFnndmd524Lu+P96f6FCpmfpOsKnFkhUyewOhOXCWs0U7XiPJzKDRXt2YeZUKh/WewSSwr1EzBFfLQHR3nGpsjeZeIOGn801OVsTirLUmKbRVA2T7iI/by+AdZWoWBlOEI85D2ChCoX4c3Cgg2OMFaGc4WfugbHa40+i1gtFO8gvElErXlMTuXAKHNmWJiedbnhKa9uIdtey7IRrwXqcjY+lXspR1a/fZzXM8DaIo5By66bk8zy9HtFxRgLRMDSZ+TA6gKhqH8SFnUkRZXaV+DtIgDM43h8UpwiFbMIbysZ68g09yaR5O4Eh2oN3jLsiyvTfadxRFPU5ezuVJ6O9xTkZ+MYseP+PqvstmWdmy6G8o7TDJ3aPK36ZKoJssBq6QgypwgleUUQtKhEZsDwk6Wz8FF8S/iB9wmlmJfGOs3wLe92sQgfEEntQ0VwtVVEvWVWb672OVsgIuZVIlW1UQQmJwjL3YdLOpyjkz51OFspTrpm4W/ivxtPipz7HBGonWfI1++6sk4RypHHAYac5H/qnrIeIgKX0QKrLO+5q2q/9y7DlfUpQfxiQfZRQjluxXeV50pX4iSR850gAtCrhWUdqJh7ivY5W4UPCBdpmvBPNwh36dp0lZ041Zmjkz51OBsUC/kMwdk8vEUEvH34gfiDy3b0jP1FcAw7C94IhwJjGENbGFPWMew0eBV4wuqmKd96ygAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle -147269151168$"
      ],
      "text/plain": [
       "-147269151168"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B.det()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Minor matrices** can also be extracted by using code ```sy.matrices.matrices.MatrixDeterminant.minor_submatrix()```, for instance, the $M_{23}$ matrix of $  B$ is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & 0 & 16 & 14 & 48 & 0\\\\0 & 31 & 0 & 0 & 33 & 43\\\\0 & 0 & 48 & 0 & 28 & 12\\\\42 & 41 & 36 & 50 & 25 & 19\\\\36 & 42 & 36 & 0 & 13 & 20\\\\0 & 11 & 16 & 27 & 36 & 19\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡0   0   16  14  48  0 ⎤\n",
       "⎢                      ⎥\n",
       "⎢0   31  0   0   33  43⎥\n",
       "⎢                      ⎥\n",
       "⎢0   0   48  0   28  12⎥\n",
       "⎢                      ⎥\n",
       "⎢42  41  36  50  25  19⎥\n",
       "⎢                      ⎥\n",
       "⎢36  42  36  0   13  20⎥\n",
       "⎢                      ⎥\n",
       "⎣0   11  16  27  36  19⎦"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sy.matrices.matrices.MatrixDeterminant.minor_submatrix(B, 1, 2) # Pythonic indexing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Cofactor matrix** is the matrix contain all cofactors of original matrix, and function ```.cofactor_matrix()``` can do the job.\n",
    "\n",
    "$$A=\\left[\\begin{array}{rrr}\n",
    "C_{11} & C_{12} & C_{13} \\\\\n",
    "C_{21} & C_{22} & C_{23} \\\\\n",
    "C_{31} & C_{32} & C_{33}\n",
    "\\end{array}\\right]=\n",
    "\\left[\\begin{array}{rrr}\n",
    "(-1)^{1+1}M_{11} & (-1)^{1+2}M_{12} & (-1)^{1+3}M_{13} \\\\\n",
    "(-1)^{2+1}M_{21} & (-1)^{2+2}M_{22} & (-1)^{2+3}M_{23} \\\\\n",
    "(-1)^{3+1}M_{31} & (-1)^{3+2}M_{32} & (-1)^{3+3}M_{33}\n",
    "\\end{array}\\right]\n",
    "$$\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}704 & -2144 & -44\\\\0 & 1446 & 0\\\\-1342 & -251 & 1078\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡ 704   -2144  -44 ⎤\n",
       "⎢                  ⎥\n",
       "⎢  0    1446    0  ⎥\n",
       "⎢                  ⎥\n",
       "⎣-1342  -251   1078⎦"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sy.matrices.matrices.MatrixDeterminant.cofactor_matrix(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> Triangular Matrix</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If $A$ is triangular matrix, cofactor expansion can be applied repetitively, the outcome will be a product of the elements on the principal diagonal. \n",
    "\n",
    "$$\n",
    "\\operatorname{det A}_{n\\times n} = \\prod_{i=1}^n a_{ii}\n",
    "$$\n",
    "\n",
    "where $a_{ii}$ is the diagonal element. \n",
    "\n",
    "Here is the proof, start with $A$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "A=\\left[\\begin{array}{cccc}\n",
    "a_{11} & a_{12} & \\cdots & a_{1 n} \\\\\n",
    "& a_{22} & \\cdots & a_{2 n} \\\\\n",
    "& & \\ddots & \\\\\n",
    "& & & a_{n n}\n",
    "\\end{array}\\right]\n",
    "$$\n",
    "Cofactor expanding on the first column, \n",
    "$$\n",
    "a_{11} \\cdot \\left[\\begin{array}{cccc}\n",
    "a_{22} & a_{22} & \\cdots & a_{2 n} \\\\\n",
    "& a_{33} & \\cdots & a_{3 n} \\\\\n",
    "& & \\ddots & \\\\\n",
    "& & & a_{n n}\n",
    "\\end{array}\\right]\n",
    "$$\n",
    "Continue the cofactor expansion\n",
    "$$\n",
    "\\operatorname{det} A=a_{11} a_{22} \\cdot \\left[\\begin{array}{cccc}\n",
    "a_{33} & a_{34} & \\cdots & a_{3 n} \\\\\n",
    "& a_{44} & \\cdots & a_{4 n} \\\\\n",
    "& & \\ddots & \\\\\n",
    "& & & a_{n n}\n",
    "\\end{array}\\right]\n",
    "$$\n",
    "Iterating the expansion, eventually\n",
    "$$\n",
    "\\text { Det } A=a_{11} \\cdots a_{n-2, n-2} \\cdot \\left[\\begin{array}{cc}\n",
    "a_{n-1, n-1} & a_{n-1, n} \\\\\n",
    "a_{n n}\n",
    "\\end{array}\\right]=a_{11} \\cdots a_{n n}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's verify with a numeric example, generate a random upper triangular matrix."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[73., 66., 34.,  9., 55.],\n",
       "       [ 0.,  6., 65., 91., 54.],\n",
       "       [ 0.,  0., 14., 15., 41.],\n",
       "       [ 0.,  0.,  0., 85., 35.],\n",
       "       [ 0.,  0.,  0.,  0., 24.]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.round(np.random.rand(5,5)*100)\n",
    "A_triu = np.triu(A); A_triu"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compute the determinant with ```np.linalg.det```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHoAAAASCAYAAACU/srTAAAE+klEQVR4nO3ZZ4hdRRQH8N/GSNRgYjegkphYiSX2Ssyq2LELfnAVuyBqNJKIKG4EsYCaolgQu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qyBuvdy7HpLat4Nmf4ko2ALs1yKMeKsvqD0VPwq3CNTYrt5QyZNWkfLacbeZheq53fVEYWCyqYL2hBeuIpGlZwfO+chmU2vwVSq5/aU1ff/KoLKtRRV8tEpb3RILVUT7caN0TpAzDhRXRs5r0hchkRwiXWovJwgM8KO6OGYboib3SWhfh2oLnVbjMSu152CL37Eix0X+JylUjPIj4v4Mo+KyyrHxl7Pj0IbLiw4WFZcQ6ROyDM0QiskJUZopiS7vuFahWkZ22icpRZyJytLC0l0SMq7WEjGxtue9j7COs7jPsr3vpcLY40XPSHKNFIrZEZMr5DLoqlwFJxqFJ/tPCxe8o3HqTCAFTG+SRzTtcXBfbV1VWvtY9JpGuxUhdScVXuhSd3VPXSqSK8Ibuim4Td8DdhKsejN/EnwcPpU9R1vkF9hSWeIRQ2g+YJk5wPll6EqeKosy6ouh/j7DY9gL5VbmsTGu4MM1zgihQLBCHdZqwuEZ5lKGSrLxFr8F/FKurBLoG/zKsUfT/BP8AvHmhcms86iIAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle 12509280.0$"
      ],
      "text/plain": [
       "12509279.999999993"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.det(A_triu)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Extract the diagonal by ```np.diag()```, then calculate the product. The same results!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHoAAAASCAYAAACU/srTAAAE+klEQVR4nO3ZZ4hdRRQH8N/GSNRgYjegkphYiSX2Ssyq2LELfnAVuyBqNJKIKG4EsYCaolgQu2DF3qJkkagYwfIhxhKNa29rMG40puqHM5d9e/e+u3vzNiCaPzzmvblzz8x/zpwy5zW1trZag/8+BuR+n4zpmIXf8TcervPuxjgHT+NzLMZCvImzC2RnaE9yiz4/lqx1S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ILVUT7caN0TpAzDhRXRs5r0hchkRwiXWovJwgM8KO6OGYboib3SWhfh2oLnVbjMSu152CL37Eix0X+JylUjPIj4v4Mo+KyyrHxl7Pj0IbLiw4WFZcQ6ROyDM0QiskJUZopiS7vuFahWkZ22icpRZyJytLC0l0SMq7WEjGxtue9j7COs7jPsr3vpcLY40XPSHKNFIrZEZMr5DLoqlwFJxqFJ/tPCxe8o3HqTCAFTG+SRzTtcXBfbV1VWvtY9JpGuxUhdScVXuhSd3VPXSqSK8Ibuim4Td8DdhKsejN/EnwcPpU9R1vkF9hSWeIRQ2g+YJk5wPll6EqeKosy6ouh/j7DY9gL5VbmsTGu4MM1zgihQLBCHdZqwuEZ5lKGSrLxFr8F/FKurBLoG/zKsUfT/BP8AvHmhcms86iIAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle 12509280.0$"
      ],
      "text/plain": [
       "12509280.0"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_diag = np.diag(A_triu)\n",
    "np.prod(A_diag)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> Properties of Determinants</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Determinants has a long list of properties, however they are mostly derived facts from cofactor expansion. So no need to memorise any of them.\n",
    "    \n",
    "1. Let $A$ be an $n×n$ square matrix. If one row of $A$ is multiplied by $k$ to produce the matrix $B$, then: $\\text{det} B = k\\, \\text{det} A$.\n",
    "2. Let $A$ be an $n×n$ square matrix. If two rows of $A$ are interchanged to produce a matrix $B$ , then $\\text{det}B=-\\text{det}A$.\n",
    "3. Let $A$ be an $n×n$ square matrix. If a multiple of one row of $A$ is added to another row to produce the matrix $B$, then $\\text{det}A=\\text{det}B$.\n",
    "4. If $A$ is an $n×n$ matrix, then $\\text{det}A^T = \\text{det}A$\n",
    "5. A square matrix $A$ is invertible if and only if $\\text{det}A\\neq 0$\n",
    "6. If $A$ and $B$ are $n×n$ matrices, then $\\text{det}AB = (\\text{det}{A})(\\text{det}{B})$\n",
    "7. If $A$ is an $n×n$ matrix and $k$ is a scalar, then $\\text{det}\\,kA=k^n\\,\\text{det}A$.\n",
    "8. If $A$ is an invertible square matrix, then $\\text{det}A^{-1}=\\frac{1}{\\det A}$.\n",
    "</font>\n",
    "\n",
    "All of them are straightforward, the key is to demonstrate them via cofactor expansion. Here is some unrigorous proofs."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font face=\"gotham\" color=\"red\">Proof of property 6</font>:\n",
    "$$\\begin{aligned}\n",
    "|A B| &=\\left|E_{p} \\cdots E_{1} B\\right|=\\left|E_{p}\\right|\\left|E_{p-1} \\cdots E_{1} B\\right|=\\cdots \\\\\n",
    "&=\\left|E_{p}\\right| \\cdots\\left|E_{1}\\right||B|=\\cdots=\\left|E_{p} \\cdots E_{1}\\right||B| \\\\\n",
    "&=|A||B|\n",
    "\\end{aligned}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font face=\"gotham\" color=\"red\">Proof of property 7</font>:\n",
    "\n",
    "Because $\\text{det} B = k\\, \\text{det} A$, one row of $A$ is multiplied by $k$ to produce $B$.Then multiply all the rows of $A$ by $k$, there will be $n$ $k$'s in front of $\\text{det} A$, which is $k^n \\text{det} A$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font face=\"gotham\" color=\"red\">Proof of property 8</font>:\n",
    "$$\\begin{aligned}\n",
    "&\\begin{aligned}\n",
    "A A^{-1} &=I \\\\\n",
    "\\left|A A^{-1}\\right| &=|I|\n",
    "\\end{aligned}\\\\\n",
    "&|A|\\left|A^{-1}\\right|=1\\\\\n",
    "&\\left|A^{-1}\\right|=\\frac{1}{|A|}\n",
    "\\end{aligned}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These properties are useful in analytical derivation of some other theorems, however not efficient in numerical computation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font face=\"gotham\" color=\"purple\"> Cramer's Rule</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If a linear system has $n$ equations and $n$ variables, an algorithm called **Cramer's Rule** can solve the system in terms of determinants as long as the solution is unique.\n",
    "$$\n",
    "A_{n\\times n}\\mathbf{b}_{n} = \\mathbf{x}_n\n",
    "$$\n",
    "\n",
    "Some convenient notations are introduced here:\n",
    "\n",
    "For any $A_{n\\times n}$ and vector $\\mathbf{b}$, denote $A_i(\\mathbf{b})$ as the matrix obtained from replacing the $i$th column of $A$ by $\\mathbf{b}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$A_{i}(\\mathbf{b})=\\left[\\begin{array}{lllll}\n",
    "\\mathbf{a}_{1} & \\cdots & \\mathbf{b} & \\cdots & \\mathbf{a}_{n}\n",
    "\\end{array}\\right]$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Cramer's Rule can solve each $x_i$ without solving the whole system\n",
    "$$x_{i}=\\frac{\\operatorname{det} A_{i}(\\mathbf{b})}{\\operatorname{det} A}, \\quad i=1,2, \\ldots, n$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"background-color:Bisque; color:DarkBlue; padding:30px;\">\n",
    "<b>Fast Proof of Cramer's Rule</b>：\n",
    "\n",
    "$$\\begin{aligned}\n",
    "A \\cdot I_{i}(\\mathbf{x}) &=A\\left[\\mathbf{e}_{1} \\quad \\cdots \\quad \\mathbf{x} \\quad \\cdots \\quad \\mathbf{e}_{n}\\right]=\\left[\\begin{array}{llllll}\n",
    "A \\mathbf{e}_{1} & \\cdots & A \\mathbf{x} & \\cdots & A \\mathbf{e}_{n}\n",
    "\\end{array}\\right] \\\\\n",
    "&=\\left[\\begin{array}{llllll}\n",
    "\\mathbf{a}_{1} & \\cdots & \\mathbf{b} & \\cdots & \\mathbf{a}_{n}\n",
    "\\end{array}\\right]=A_{i}(\\mathbf{b})\n",
    "\\end{aligned}$$\n",
    "    \n",
    "where $I_i(\\mathbf{x})$ is an identity matrix whose $i$th column replaced by $\\mathbf{x}$. With determinant's property,\n",
    "$$(\\operatorname{det} A)\\left(\\operatorname{det} I_{i}(\\mathbf{x})\\right)=\\operatorname{det} A_{i}(\\mathbf{b})$$\n",
    "\n",
    "$\\text{det}I_{i}(\\mathbf{x})=x_i$, can be shown by cofactor expansion.\n",
    "</div> "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> A NumPy Example On Cramer's Rule</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider the system\n",
    "$$\\begin{aligned}\n",
    "&2 x-y+3 z=-3\\\\\n",
    "&3 x+3 y-z=10\\\\\n",
    "&-x-y+z=-4\n",
    "\\end{aligned}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You have surely known several ways to solve it, but let's test if Cramer's rule works. \n",
    "\n",
    "Input the matrices into NumPy arrays."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[2, -1, 3], [3, 3, -1], [-1, -1, 1]])\n",
    "b = np.array([-3, 10, -4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "A_1b = np.copy(A) # Python variable is a reference tag\n",
    "A_1b[:,0]=b\n",
    "\n",
    "A_2b = np.copy(A)\n",
    "A_2b[:,1]=b\n",
    "\n",
    "A_3b = np.copy(A)\n",
    "A_3b[:,2]=b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "According to Cramer's rule:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw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r0Y4F76mZDHWlnH7EjZZU6e9MW6lKxQ3OW2A/PMjv5wMGJPEV0PjHoNzfc8xYOxwQR+KdcI7bWFguHWYuTxAq3DrfnKJ9OqqK5zkYuwwKRJ14pXqQa2WCHPSl1sqETzNbdcSZW4e1i2xX5jvq9yCs27iZMFYOdZWLyga9yDGjFgDj8skDs+Z4rQo/FYrblvdgcIxLFrxb8j0YmJHK4iFHzeEezo7aiGzZ0gqm6O85jxER5RWxtg76c86jzxWGA4/BxQYWqZqw4vPAAbumuKrvMWLu/DZ3gOuHI08STD/lH2xaJk0ULOqNXIU4T25wPdluREuwJNlTNSLFQLpVxUvKP+W0Tye/pIuCtcju1KMtUn9+tmj3BhqoZjyflhF95jkq8/a+oTp8ePXJZi/68il051d6jR8f5F5lJQAS2kWkcAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle \\left( 1.0, \\  2.0, \\  -1.0\\right)$"
      ],
      "text/plain": [
       "(1.0000000000000007, 1.9999999999999982, -1.0000000000000002)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1 = np.linalg.det(A_1b)/np.linalg.det(A)\n",
    "x2 = np.linalg.det(A_2b)/np.linalg.det(A)\n",
    "x3 = np.linalg.det(A_3b)/np.linalg.det(A)\n",
    "(x1, x2, x3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can verify the results by NumPy built-in function ```np.linalg.solve```."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  2., -1.])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.solve(A, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or in a straightforward way $A^{-1}b$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  2., -1.])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.inv(A)@b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All results are the same!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, remember that Cramer's rule is rarely carried out in practice when solving system of equations, since computational cost (measured by the number of floating operations (flop), are much higher than Gaussian-Jordon elimination."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font face=\"gotham\" color=\"purple\"> A Determinant Formula For $A^{-1}$</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An alternative algorithm for $A^{-1}$ is\n",
    "$$A^{-1}=\\frac{1}{\\operatorname{det} A}\\left[\\begin{array}{cccc}\n",
    "C_{11} & C_{21} & \\cdots & C_{n 1} \\\\\n",
    "C_{12} & C_{22} & \\cdots & C_{n 2} \\\\\n",
    "\\vdots & \\vdots & & \\vdots \\\\\n",
    "C_{1 n} & C_{2 n} & \\cdots & C_{n n}\n",
    "\\end{array}\\right]$$\n",
    "\n",
    "where the matrix of cofactors on RHS is the **adjugate** matrix, SymPy function is ```sy.matrices.matrices.MatrixDeterminant.adjugate```. And this is the transpose of the _cofactor matrix_ which we computed using ```sy.matrices.matrices.MatrixDeterminant.cofactor_matrix```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font face=\"gotham\" color=\"purple\"> A SymPy Example</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate a random matrix with $20\\%$ of zero elements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0 & 0 & -4 & 2\\\\-1 & 0 & 0 & -2 & 0\\\\-3 & 0 & 1 & 5 & 0\\\\3 & 1 & 4 & 0 & -1\\\\1 & 1 & -2 & 1 & -4\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡1   0  0   -4  2 ⎤\n",
       "⎢                 ⎥\n",
       "⎢-1  0  0   -2  0 ⎥\n",
       "⎢                 ⎥\n",
       "⎢-3  0  1   5   0 ⎥\n",
       "⎢                 ⎥\n",
       "⎢3   1  4   0   -1⎥\n",
       "⎢                 ⎥\n",
       "⎣1   1  -2  1   -4⎦"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = sy.randMatrix(5, min=-5, max = 5, percent = 80); A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compute the adjugate matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-6 & -50 & -24 & 4 & -4\\\\221 & -39 & 140 & 18 & 106\\\\-33 & 35 & -8 & 22 & -22\\\\3 & -37 & 12 & -2 & 2\\\\71 & -49 & 36 & -6 & 6\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡-6   -50  -24  4   -4 ⎤\n",
       "⎢                      ⎥\n",
       "⎢221  -39  140  18  106⎥\n",
       "⎢                      ⎥\n",
       "⎢-33  35   -8   22  -22⎥\n",
       "⎢                      ⎥\n",
       "⎢ 3   -37  12   -2   2 ⎥\n",
       "⎢                      ⎥\n",
       "⎣71   -49  36   -6   6 ⎦"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_adjugate = sy.matrices.matrices.MatrixDeterminant.adjugate(A); A_adjugate "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can verify if this really the adjugate of $A$, we pick element of $(1, 3), (2, 4), (5, 4)$ of $A$ to compute the cofactors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAACwAAAASCAYAAAAg9DzcAAABqElEQVR4nOXWP0hWURgG8N9Xjm4NLTm0REGLDU4OohS0NBROUVG5BSI0SILy0eQSIQ4FFRa6OwiRS1EJTkFDSEmCGKhEBoFi0R8dzqE+rvfe74ZXQXzgcA/3fc/zPrw89z23Uq1W7SUcKJHrELowjo9YxzdM4fp/1LqEjbi6ksGGMpRGdOIelvACCziM83iIszFnI4ejCcNYRWNaQpkdnsU5HMFF3MI1HMcnXBDEZ6GCEazgflZSmYKfYwJ/Eu+XawS05ZzvRjuuYi0rqUzBefgZn78y4icwiCG8yiPaDcENuBz3zzLio4Ln+4qQ7TQGcRJPMZkSH0AzWoXJkotkh+f9GylF1lgd/m7cxHthXCXRInT1DqbriWVrh+fwvcjBiMWc2A3BkzPowNeU2qPCdOkvWjApuKPowTrowV28i5yfU3IacSzus5r0IK6hyLkjHu4VfPsWp/ElI+8HHmXETgm+nsIHNXYpW3A/buMNzthqg1qsS7l6I6qC4CfCLfkXZQq+Ioj9jdfCB5fEPB5vp0iZgo/G50HRbyl4aZuCK/v593JXsOcEbwLulF2JKcXKKQAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle -24$"
      ],
      "text/plain": [
       "-24"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAABsAAAASCAYAAACq26WdAAABhklEQVR4nN3VMUhVURzH8c8TIUjafBA4BNpQixWBIEEWtFSSiQ1ONZWDkxrV0PBoqyGi9nDIycBoaDDJ4YFgQ6MOPkJBIePpUKLhUA3nvLwc7Il0KegPl3PP/3/u/3vv+f/O/xZKpZK/ZQ3J/BqeoYwv+IEXe+S4jEksYwsfMY7OdGFjMr+PE9iIDx/bA/QQd7CGV6jiKHrQh+vZl01hQxFSQRem64AO4zZW0Y7Pmdh5vMODerB6yVM7IpRhNgHV8nxFMetMa7YfW8A2OtCcxM7iEKayzvTL9mPruIvHmBNqtoY2XMFbDOQFgydYxHPczPgrGJVs759sI0GJL2PiNjThtCD/MTzKC3ZOkP5rDEfAJj6gFysYQWsesO447qbgTbyP+U/lATsQx+Jv4jX/dh6wchxvoSWJXcQZfMNMzZmq8Wq8CB2C0ONG431V6BoEYUzhAuYxgU84LmxxAfeE47Ar7CRuJL5WO0VeysC+4xIG0S+I4qBw/t7gqdCgf1nhX/5i/h/YT0sQUN2iImjXAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle 18$"
      ],
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAB8AAAASCAYAAACjMAXnAAABYElEQVR4nMXVv2sUQRTA8c9FI1whaSStpLCLhYU/IVFIJKCVQRtbEayv9ZCDFOL/YROSdFFUYpMm4QrBIkUK5YqgqIeXoMQEI6aYWRiGiHewSx4sb+f92O++94aZWqvVclwyVNF3J7CEz9iP+g1upUEnKwA3MYculiP4DC7gBl5WBb8XwSuYxY/MP5wuymz7EJ5hF/ePAMPvdFFm5dcwhkX0cBvj2EMba3lCmfCLUX/BO5zP/Ku4i2+Focy2j0b9CHVM47RQ/WtMYiFNyOEd/B3geZ7knoi6JlT4Fj+xgTvYwnVcLRLytn8QZtSvfEree1F/xPss7pdQ/QNcEuefw6cGAOeyGfX2P/zFz9ULQ5kzX8UBzuHUEf7xqDtVwLuYxwieZL6bmMEOXhXGsk+4Bi7jsbC72zgrbLg/eCgZS9nwrxHejMArwkn3Ak+xngZXcbF8FzrQ+F9gVVdqX3Ks8EN5xk0jrgj3ogAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle -6$"
      ],
      "text/plain": [
       "-6"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(-1)**(1+3) * sy.matrices.matrices.MatrixDeterminant.minor(A, 2, 0) # transpose the index\n",
    "(-1)**(2+4) * sy.matrices.matrices.MatrixDeterminant.minor(A, 3, 1)\n",
    "(-1)**(5+4) * sy.matrices.matrices.MatrixDeterminant.minor(A, 3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Adjugate is the transpose of cofactor matrix, thus we reverse the row and column index when referring to the elements in mind. They are correct as in adjugate matrix, so we have obtained the correct one."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To make results tidy, we need some extra steps."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ```sy.N()``` is for converting to float approximation, i.e. if you don't like fractions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-0.0484 & -0.4032 & -0.1935 & 0.0323 & -0.0323\\\\1.7823 & -0.3145 & 1.129 & 0.1452 & 0.8548\\\\-0.2661 & 0.2823 & -0.0645 & 0.1774 & -0.1774\\\\0.0242 & -0.2984 & 0.0968 & -0.0161 & 0.0161\\\\0.5726 & -0.3952 & 0.2903 & -0.0484 & 0.0484\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡-0.0484  -0.4032  -0.1935  0.0323   -0.0323⎤\n",
       "⎢                                           ⎥\n",
       "⎢1.7823   -0.3145   1.129   0.1452   0.8548 ⎥\n",
       "⎢                                           ⎥\n",
       "⎢-0.2661  0.2823   -0.0645  0.1774   -0.1774⎥\n",
       "⎢                                           ⎥\n",
       "⎢0.0242   -0.2984  0.0968   -0.0161  0.0161 ⎥\n",
       "⎢                                           ⎥\n",
       "⎣0.5726   -0.3952  0.2903   -0.0484  0.0484 ⎦"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_det = A.det()\n",
    "A_inv = (1/A_det)*A_adjugate\n",
    "round_expr(sy.N(A_inv), 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now again, we can verify the results with ```.inv()```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}-0.0484 & -0.4032 & -0.1935 & 0.0323 & -0.0323\\\\1.7823 & -0.3145 & 1.129 & 0.1452 & 0.8548\\\\-0.2661 & 0.2823 & -0.0645 & 0.1774 & -0.1774\\\\0.0242 & -0.2984 & 0.0968 & -0.0161 & 0.0161\\\\0.5726 & -0.3952 & 0.2903 & -0.0484 & 0.0484\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡-0.0484  -0.4032  -0.1935  0.0323   -0.0323⎤\n",
       "⎢                                           ⎥\n",
       "⎢1.7823   -0.3145   1.129   0.1452   0.8548 ⎥\n",
       "⎢                                           ⎥\n",
       "⎢-0.2661  0.2823   -0.0645  0.1774   -0.1774⎥\n",
       "⎢                                           ⎥\n",
       "⎢0.0242   -0.2984  0.0968   -0.0161  0.0161 ⎥\n",
       "⎢                                           ⎥\n",
       "⎣0.5726   -0.3952  0.2903   -0.0484  0.0484 ⎦"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round_expr(sy.N(A.inv()), 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or We can show by difference."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}0 & 0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0 & 0\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "⎡0  0  0  0  0⎤\n",
       "⎢             ⎥\n",
       "⎢0  0  0  0  0⎥\n",
       "⎢             ⎥\n",
       "⎢0  0  0  0  0⎥\n",
       "⎢             ⎥\n",
       "⎢0  0  0  0  0⎥\n",
       "⎢             ⎥\n",
       "⎣0  0  0  0  0⎦"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_inv-A.inv()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So Cramer's rule indeed works perfectly."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"background-color:Bisque; color:DarkBlue; padding:30px;\">\n",
    "<b>Short Proof of $A^{-1}$ Formula With Determinants</b><br>\n",
    "We define $x$ the $j$th column of $A^{-1}$ which satisfies\n",
    "$$\n",
    "Ax= e_j\n",
    "$$\n",
    "\n",
    "and $e_j$ is the $j$th column of an identity matrix, and $j$th entry of $x$ is the $(i,j)$-entry of $A^{-1}$. By Cramer's rule,\n",
    "\n",
    "$$\\left\\{(i, j) \\text { -entry of } A^{-1}\\right\\}=x_{i}=\\frac{\\operatorname{det} A_{i}\\left(\\mathbf{e}_{j}\\right)}{\\operatorname{det} A}$$\n",
    "\n",
    "The cofactor expansion along column $i$ of $A_i(e_j)$,\n",
    "$$\\operatorname{det} A_{i}\\left(\\mathbf{e}_{j}\\right)=(-1)^{i+j} \\operatorname{det} A_{j i}=C_{j i}$$\n",
    "</div>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
